<template>
  <div class="login">
    <el-form class="loginBox" ref="form" :model="form">
      <el-form-item label="账号">
        <el-input v-model="form.a_user"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="form.a_pass" show-password></el-input>
      </el-form-item>
      <el-form-item label="身份类型">
        <el-radio-group v-model="form.type">
          <el-radio label="2">学生</el-radio>
          <el-radio label="0">管理员</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item style="text-align: center">
        <el-button type="primary" @click="login">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
// import axios from "axios";
// 引用路由
import router from "../router/index";
export default {
  methods: {
    login: function () {
      this.$axios({
        url: "/loginInfo",
        method: "post",
        data: this.form,
      }).then((res) => {
        // console.log(res.data.error);
        if (res.data.error == 0) {
          // 将token存到会话存储里面
          sessionStorage.setItem("token", res.data.token);
          sessionStorage.setItem("userName", JSON.stringify(res.data.data));
          // 将列表信息存到session中
          sessionStorage.setItem("power", JSON.stringify(res.data.power));
          // 动态路由
          res.data.power.forEach((v) => {
            router.addRoute("Home", {
              path: v.href,
              component: () => import(`../views/${v.com}.vue`),
            });
          });

          this.$message.success("登录成功");
          this.$router.push("/home");
        } else {
          this.$message.error("登录失败，账号名密码错误  !");
        }
      });
    },
  },
  data: function () {
    return {
      form: {
        a_user: "",
        a_pass: "",
        type: "2",
      },
    };
  },
};
</script>

<style lang="less" scoped>
* {
  margin: 0;
  padding: 0;
}
html,
body {
  width: 100%;
  height: 100%;
}
.login {
  background: #399;
  width: 100%;
  height: 100%;
  background-image: url("../assets/images/login_bg.jpg");
  .loginBox {
    padding: 40px 40px 40px 40px;
    border: 1px solid #fff;
    width: 400px;
    height: 300px;
    margin: 0 auto;
    border-radius: 10px;
    position: relative;
    top: 20%;

    .el-button{
      color: #fff;
    background-color: #409eff;
    border-color: #409eff;
    width: 100px;
    height: 40px;
}
    }
}
</style>